package com.szbmyoubao.app.ui.main.mine

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.ellecity06.common.base.BaseMvvmFragment
import com.ellecity06.common.ktx.addGridDecoration
import com.ellecity06.common.ktx.launchActivity
import com.ellecity06.cwhiskymall.BuildConfig
import com.ellecity06.cwhiskymall.databinding.FragmentMineBinding
import com.szbmyoubao.app.adapter.MineBlockAdapter
import com.szbmyoubao.app.adapter.ToolsAdapter
import com.szbmyoubao.app.bean.MenuItem
import com.szbmyoubao.app.bean.ToolsItem
import com.szbmyoubao.app.ui.service_setting.ServiceSettingActivity

/**
 * @author ellecity06
 * @time 2024/10/22 14:53
 * @des 我的
 */
class MineFragment : BaseMvvmFragment<FragmentMineBinding, MineViewModel>(), View.OnClickListener {
    private val adapter: ToolsAdapter by lazy { ToolsAdapter() }
    private val otherMenuAdapter: MineBlockAdapter by lazy { MineBlockAdapter() }


    companion object {

        @JvmStatic
        fun newInstance(): MineFragment {
            return MineFragment()
        }

    }

    override fun lazyInit() {
        initAiMenu()
        initOtherMenu()
        initEvent()
    }














    override fun onResume() {
        super.onResume()
    }

    private fun fetchUserInfo() {
        lifecycleScope.launchWhenStarted {
            viewModel.sendUiIntent(MineIntent.FetchUserInfo)
        }
    }

    private fun initEvent() {
        if (!BuildConfig.SERVER_RELEASE) {
            binding.ivUser.setOnLongClickListener {
                launchActivity<ServiceSettingActivity>()
                true
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            fetchUserInfo()
        }
        binding.tvLogout.setOnClickListener(this)
        binding.tvCopy.setOnClickListener(this)

        binding.ivUser.setOnClickListener(this)

        binding.tvInviteDetail.setOnClickListener(this)
    }

    private fun initAiMenu() {
        binding.rvAi.layoutManager = GridLayoutManager(requireActivity(), 4)
        binding.rvAi.addGridDecoration()
        binding.rvAi.adapter = adapter
        val list = mutableListOf<ToolsItem.ToolsChildItem>()
//        list.add(ToolsItem.ToolsChildItem(name = "极速数字人", icon = R.mipmap.icon_v_tool_jsszr))

        adapter.submitList(list)
    }

    private fun setOtherMenuList() {
        val list = mutableListOf<MenuItem>()
        otherMenuAdapter.submitList(list)
    }

    private fun initOtherMenu() {
        binding.rvOtherMenu.layoutManager = LinearLayoutManager(requireActivity())
        binding.rvOtherMenu.adapter = otherMenuAdapter
        setOtherMenuList()
    }

    override fun inflateBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ): FragmentMineBinding {
        return FragmentMineBinding.inflate(inflater, container, false)
    }

    override fun onClick(p0: View?) {
    }
}